<%--
  Created by IntelliJ IDEA.
  User: 13095
  Date: 2024/10/16
  Time: 16:25
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<c:set value="${pageContext.request.contextPath}" var="path"></c:set>
<script src="${path}/static/js/jquery-3.2.1.min.js"></script>
<link href="${path}/static/css/load.css" rel="stylesheet">
<script src="https://api.map.baidu.com/api?v=2.0&ak=5k8Rpa7Bi5HSNNuhjT0EqI6GvMfdQaxe"></script>
<script>

</script>
<html>
<head>
    <title>接取任务</title>
</head>
<body>
<div class="loader" style="width: 120px;height: 120px;margin: 0 auto;" >
</div>
        <input type="hidden" value="${requestScope.orders.id}" id="ordersid" >
        <input type="hidden" value="${requestScope.orders.task.longitude}" id="longitude">
        <input type="hidden" value="${requestScope.orders.task.latitude}" id="latitude">
        <input type="hidden" value="${requestScope.orders.task.distance}" id="distance">
        <input type="hidden" id="esti" name="estimatedtime">

</body>
<script>
        $(document).ready(function () {
                $(".loader").show();
                // 获取DOM元素并缓存，同时将相关值转换为数值类型
                const $ordersid = $('#ordersid');
                const $longitude = $('#longitude');
                const $latitude = $('#latitude');
                const $distance = $('#distance');
                const $esti = $('#esti');
                const longitudeVal = Number($longitude.val());
                const latitudeVal = Number($latitude.val());
                const distanceVal = Number($distance.val());

                // 获取位置
                navigator.geolocation.getCurrentPosition(function (position) {
                        const lat = position.coords.latitude;
                        const lng = position.coords.longitude;
                        const myLocation = { lng: lng, lat: lat };
                        const targetLocation = { lng: longitudeVal, lat: latitudeVal };
                        //距离计算
                        const distance2 = calculateDistance(myLocation, targetLocation) / 1000;
                        // 更新距离值
                        $distance.val((distance2 + distanceVal).toFixed(2));
                        const distanceTotal = Number($distance.val());
                        const minus = (distanceTotal * 2).toFixed(0);
                        $esti.val(minus);
                        // 跳转页面
                        location.href = "${path}/runner/apt?minus=" + $esti.val() + "&id=" + $ordersid.val();
                }, function (error) {
                        // 处理定位错误，这里是弹出提示框，可以根据需求修改
                        alert('定位失败：'+error.message);
                }, { enableHighAccuracy: true });
        });

        //距离计算函数
        function calculateDistance(pos1, pos2) {
                const R = 6371; // 地球半径（单位：千米）
                const dLat = toRad(pos2.lat - pos1.lat);
                const dLon = toRad(pos2.lng - pos1.lng);
                const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                        Math.cos(toRad(pos1.lat)) * Math.cos(toRad(pos2.lat)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
                const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                return R * c * 1000; // 返回距离（单位：米）
        }

        function toRad(deg) {
                return deg * (Math.PI / 180);
        }
</script>
</html>
